package com.sjgs.gis.service;

import java.util.List;

import javax.persistence.criteria.JoinType;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.github.jhipster.service.QueryService;

import com.sjgs.gis.domain.War;
import com.sjgs.gis.domain.*; // for static metamodels
import com.sjgs.gis.repository.WarRepository;
import com.sjgs.gis.service.dto.WarCriteria;
import com.sjgs.gis.service.dto.WarDTO;
import com.sjgs.gis.service.mapper.WarMapper;

/**
 * Service for executing complex queries for {@link War} entities in the database.
 * The main input is a {@link WarCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link WarDTO} or a {@link Page} of {@link WarDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class WarQueryService extends QueryService<War> {

    private final Logger log = LoggerFactory.getLogger(WarQueryService.class);

    private final WarRepository warRepository;

    private final WarMapper warMapper;

    public WarQueryService(WarRepository warRepository, WarMapper warMapper) {
        this.warRepository = warRepository;
        this.warMapper = warMapper;
    }

    /**
     * Return a {@link List} of {@link WarDTO} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<WarDTO> findByCriteria(WarCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<War> specification = createSpecification(criteria);
        return warMapper.toDto(warRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link WarDTO} which matches the criteria from the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<WarDTO> findByCriteria(WarCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<War> specification = createSpecification(criteria);
        return warRepository.findAll(specification, page)
            .map(warMapper::toDto);
    }

    /**
     * Return the number of matching entities in the database.
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(WarCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<War> specification = createSpecification(criteria);
        return warRepository.count(specification);
    }

    /**
     * Function to convert {@link WarCriteria} to a {@link Specification}
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching {@link Specification} of the entity.
     */
    protected Specification<War> createSpecification(WarCriteria criteria) {
        Specification<War> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getId(), War_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), War_.name));
            }
            if (criteria.getVersion() != null) {
                specification = specification.and(buildStringSpecification(criteria.getVersion(), War_.version));
            }
            if (criteria.getSrcUrl() != null) {
                specification = specification.and(buildStringSpecification(criteria.getSrcUrl(), War_.srcUrl));
            }
            if (criteria.getFkey() != null) {
                specification = specification.and(buildStringSpecification(criteria.getFkey(), War_.fkey));
            }
        }
        return specification;
    }
}
